package com.onedec.materials.service.serviceimp;

import com.onedec.materials.client.ProjectClient;
import com.onedec.materials.entity.OnedecMaterialList;
import com.onedec.materials.entity.OnedecProject;
import com.onedec.materials.entity.OnedecProjectExample;
import com.onedec.materials.mapper.OnedecMaterialListMapper;
import com.onedec.materials.mapper.OnedecProjectMapper;
import com.onedec.materials.service.MaterialListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MaterialListScerviceImp implements MaterialListService {
    @Autowired
    private OnedecMaterialListMapper materialListMapper;
    @Autowired
    private OnedecProjectMapper onedecProjectMapper;

    @Override

    public int addMaterialLists(List<OnedecMaterialList> onedecMaterialListList) {
        final OnedecProjectExample example = new OnedecProjectExample();
        example.createCriteria().andProjectNullEqualTo(onedecMaterialListList.get(0).getMaterialListBak1());
        final List<OnedecProject> projects = onedecProjectMapper.selectByExample(example);
        final OnedecProject project = projects.get(0);
        onedecMaterialListList.forEach(e -> {
            e.setProjectId(project.getProjectId());
        });
        int i = materialListMapper.addMaterisalLsit(onedecMaterialListList);
        return i;
    }

    public List<Map<String,Object>> selectMaterialListByProject(String projectNo, Integer inPhase) {
        final HashMap<String, Object> map = new HashMap<>();
        map.put("projectNo", projectNo);
        map.put("inPhase", inPhase);
        final List<Map<String, Object>> maps = materialListMapper.selectMaterialListByProject(map);
        return maps;
    }
}
